package jp.co.ricoh.melody.views.fragments;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.FragmentManager;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;

import jp.co.ricoh.melody.R;

/**
 * Copyright (C) 2016 RICOH Co.,LTD.
 * All rights reserved.
 * <p/>
 * Dialog when click button Cancel
 */
public class NoSaveDialogFragment extends DialogFragment implements View.OnClickListener {

    private Activity mActivity;
    private Button mNoBtn;
    private Button mSaveBtn;
    private boolean shown = false;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_dialog_no_save, container, false);
        init(view);
        return view;
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        // Set dialog fragment without title.
        mActivity = getActivity();
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().setBackgroundDrawableResource(R.drawable.common_dialog);
        dialog.setCanceledOnTouchOutside(false);
        return dialog;
    }

    /**
     * init view
     *
     * @param v
     */
    public void init(View v) {
        mNoBtn = (Button) v.findViewById(R.id.bt_no);
        mNoBtn.setOnClickListener(this);
        mSaveBtn = (Button) v.findViewById(R.id.bt_save);
        mSaveBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_no: {
                dismiss();
                break;
            }
            case R.id.bt_save: {
                dismiss();
                mActivity.finish();
                break;
            }
            default:
                break;
        }
    }

    @Override
    public void show(FragmentManager manager, String tag) {
        if (shown) return;
        super.show(manager, tag);
        shown = true;
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        shown = false;
        super.onDismiss(dialog);
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        // Set size for dialog.
        if (dialog != null) {
            dialog.getWindow().setLayout(getResources().getDimensionPixelOffset(R.dimen.dialog_confirm_width),
                    getResources().getDimensionPixelOffset(R.dimen.dialog_confirm_height));
        }
    }

}
